package com.hl.services.system.user.mapper;


import com.hl.framework.core.common.pojo.PageResult;
import com.hl.framework.db.mybatis.mapper.BaseMapperX;
import com.hl.framework.db.mybatis.query.LambdaQueryWrapperX;
import com.hl.services.system.user.domain.AdminUserDO;
import com.hl.services.system.user.domain.vo.user.UserPageReqVO;
import com.hl.services.system.user.domain.vo.user.UserSimpleRespVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;

@Mapper
public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {

    default AdminUserDO selectByUsername(String username) {
        return selectOne(AdminUserDO::getUsername, username);
    }

    default AdminUserDO selectByEmail(String email) {
        return selectOne(AdminUserDO::getEmail, email);
    }

    default AdminUserDO selectByMobile(String mobile) {
        return selectOne(AdminUserDO::getMobile, mobile);
    }

    default PageResult<AdminUserDO> selectPage(UserPageReqVO reqVO, Collection<Long> deptIds, Collection<Long> userIds) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AdminUserDO>()
                .likeIfPresent(AdminUserDO::getUsername, reqVO.getUsername())
                .likeIfPresent(AdminUserDO::getMobile, reqVO.getMobile())
                .eqIfPresent(AdminUserDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(AdminUserDO::getCreateTime, reqVO.getCreateTime())
                .inIfPresent(AdminUserDO::getDeptId, deptIds)
                .inIfPresent(AdminUserDO::getId, userIds)
                .orderByDesc(AdminUserDO::getId));
    }

    default List<AdminUserDO> selectListByNickname(String nickname) {
        return selectList(new LambdaQueryWrapperX<AdminUserDO>().like(AdminUserDO::getNickname, nickname));
    }

    default List<AdminUserDO> selectListByStatus(Integer status) {
        return selectList(AdminUserDO::getStatus, status);
    }

    default List<AdminUserDO> selectListByDeptIds(Collection<Long> deptIds) {
        return selectList(AdminUserDO::getDeptId, deptIds);
    }

    @Select("SELECT id,username,PASSWORD,nickname,remark,dept_id,post_ids,email,mobile,sex,avatar,STATUS,login_ip,login_date,tenant_id,create_time,update_time,creator,updater,deleted FROM system_users WHERE deleted = 0 AND (STATUS = 0 AND deleted = 0 AND id NOT IN (1)) ")
    List<UserSimpleRespVO> selectListBySql();

//    default List<AdminUserDO> selectList(AdminUserQueryDTO query) {
//        return selectList(new LambdaQueryWrapperX<AdminUserDO>()
//                .eqIfPresent(AdminUserDO::getStatus,query.getStatus())
//                .likeIfPresent(AdminUserDO::getNickname, query.getNickname()));
//    }

    @Delete("DELETE FROM system_users WHERE id = #{id}")
    void deleteUserById(Long id);
}
